<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <!-- <el-form-item label="收益金额" prop="storeFinancialMoney">
        <el-input
          v-model="queryParams.storeFinancialMoney"
          placeholder="请输入收益金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="订单标识—外键" prop="storeFinancialOrderId">
        <el-input
          v-model="queryParams.storeFinancialOrderId"
          placeholder="请输入订单标识—外键"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="用户标识—外键" prop="storeFinancialUserId">
        <el-input
          v-model="queryParams.storeFinancialUserId"
          placeholder="请输入用户标识—外键"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="店铺财务记录时间" prop="storeFinancialTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.storeFinancialTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择店铺财务记录时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="备注" prop="storeFinancialContent">
        <el-input
          v-model="queryParams.storeFinancialContent"
          placeholder="请输入备注"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> --><!--  0未提现 1已提现 2待审核 3已拒绝 -->
      <el-form-item label="提现状态" prop="storeFinancialWithdrawalState">
        <!-- <el-input
          v-model="queryParams.storeFinancialWithdrawalState"
          placeholder="请输提现状态"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select v-model="queryParams.storeFinancialWithdrawalState"
          placeholder="请选择提现状态"
          size="small">
          <el-option :value="0" label="未提现"></el-option>
          <el-option :value="1" label="已提现"></el-option>
          <el-option :value="2" label="待审核"></el-option>
          <el-option :value="3" label="已拒绝"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:financial:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:financial:edit']"
        >修改</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:financial:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:financial:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="financialList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" /><!--
      <el-table-column label="店铺财务标识" align="center" prop="storeFinancialId" /> -->
      <el-table-column label="收益金额(元)" align="center" prop="storeFinancialMoney" >
        <template slot-scope="scope">
          {{scope.row.storeFinancialMoney +'元'}}
        </template>
      </el-table-column>
      <el-table-column label="订单号" align="center" prop="storeFinancialOrderId" >
        <template slot-scope="scope">
          {{scope.row.orders != null ? scope.row.orders.ordersCode : '暂无'}}
        </template>
      </el-table-column>
      <el-table-column label="用户" align="center" prop="storeFinancialUserId" >
        <template slot-scope="scope">
          {{scope.row.personage != null ? scope.row.personage.username : '暂无'}}
        </template>
      </el-table-column>
      <el-table-column label="店铺财务记录时间" align="center" prop="storeFinancialTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.storeFinancialTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="storeFinancialContent" :show-overflow-tooltip="true"/>
      <el-table-column label="提现状态" align="center" prop="storeFinancialWithdrawalState" >
        <template slot-scope="scope"><!--  0未提现 1已提现 2待审核 3已拒绝 -->
          <!-- {{scope.row.storeFinancialWithdrawalState == 1}} -->
          <span v-if="scope.row.storeFinancialWithdrawalState == 0">
            未提现
          </span>
          <span v-if="scope.row.storeFinancialWithdrawalState == 1">
            已提现
          </span>
          <span v-if="scope.row.storeFinancialWithdrawalState == 2">
            待审核
          </span>
          <span v-if="scope.row.storeFinancialWithdrawalState == 3">
            已拒绝
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
         <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:financial:edit']"
          >修改</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:financial:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改店铺财务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="收益金额" prop="storeFinancialMoney" v-if="form.storeFinancialId == null || form.storeFinancialId == ''">
          <el-input v-model="form.storeFinancialMoney" placeholder="请输入收益金额" >
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="订单号" prop="storeFinancialOrderId" v-if="form.storeFinancialId == null || form.storeFinancialId == ''">
          <!-- <el-input v-model="form.storeFinancialOrderId" placeholder="请输入订单标识—外键" /> -->
          <el-select v-model="form.storeFinancialOrderId" placeholder="请选择订单号" style="width: 100%;">
            <el-option v-for="(item, index) in orderList" :key="index" :label="item.ordersCode" :value="item.ordersId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户" prop="storeFinancialUserId" v-if="form.storeFinancialId == null || form.storeFinancialId == ''">
          <!-- <el-input v-model="form.storeFinancialUserId" placeholder="请选择用户" /> -->
          <el-select v-model="form.storeFinancialUserId" placeholder="请选择用户" style="width: 100%;">
            <el-option v-for="(item, index) in personageLists" :key="index" :label="item.username" :value="item.uid"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="记录时间" prop="storeFinancialTime" v-if="form.storeFinancialId == null || form.storeFinancialId == ''">
          <el-date-picker clearable size="small" style="width: 100%"
            v-model="form.storeFinancialTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择店铺财务记录时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="storeFinancialContent">
          <el-input type="textarea" v-model="form.storeFinancialContent" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="提现状态" prop="storeFinancialWithdrawalState">
          <!-- <el-input v-model="form.storeFinancialWithdrawalState" placeholder="请输入提现状态" /> -->
          <el-select v-model="form.storeFinancialWithdrawalState" style="width: 100%;" placeholder="请选择提现状态" ><!--  0未提现 1已提现 2待审核 3已拒绝 -->
            <el-option :value="0" label="未提现"></el-option>
            <el-option :value="1" label="已提现"></el-option>
            <el-option :value="2" label="待审核"></el-option>
            <el-option :value="3" label="已拒绝"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFinancial, getFinancial, delFinancial, addFinancial, updateFinancial, exportFinancial } from "@/api/system/financial";
import { listOrders } from "@/api/system/orders";
import { listPersonage } from "@/api/system/personage";

export default {
  name: "Financial",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 店铺财务表格数据
      financialList: [],
      // 订单
      orderList: [],
      // 用户集合
      personageLists: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeFinancialMoney: undefined,
        storeFinancialOrderId: undefined,
        storeFinancialUserId: undefined,
        storeFinancialTime: undefined,
        storeFinancialContent: undefined,
        storeFinancialWithdrawalState: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        /* storeFinancialContent: [
          { required: true, message: "话题图片不能为空", trigger: "blur" }
        ], */
        storeFinancialWithdrawalState: [
          { required: true, message: "提现状态不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getOrdersList();
    this.getPersonageList();
  },
  methods: {
    getPersonageList(){
      listPersonage().then(response =>{
        this.personageLists = response.rows;
      })
    },
    getOrdersList(){
      listOrders().then(response =>{
        this.orderList = response.rows;
      })
    },
    /** 查询店铺财务列表 */
    getList() {
      this.loading = true;
      listFinancial(this.queryParams).then(response => {
        this.financialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        storeFinancialId: undefined,
        storeFinancialMoney: undefined,
        storeFinancialOrderId: undefined,
        storeFinancialUserId: undefined,
        storeFinancialTime: undefined,
        storeFinancialContent: undefined,
        storeFinancialWithdrawalState: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.storeFinancialId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加店铺财务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const storeFinancialId = row.storeFinancialId || this.ids
      getFinancial(storeFinancialId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改店铺财务";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.storeFinancialId != undefined) {
            updateFinancial(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addFinancial(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const storeFinancialIds = row.storeFinancialId || this.ids;
      this.$confirm('是否确认删除店铺财务编号为"' + storeFinancialIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFinancial(storeFinancialIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有店铺财务数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportFinancial(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
